package com.zyl.points.service;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zyl.points.bean.Question;
import com.zyl.points.bean.Task;
import com.zyl.points.util.DBHelper;

/**
 * 题库
 * 
 * @param
 * @return
 */
public class TaskService {

	private DBHelper helper;
	public TaskService(Context context) {
		helper = new DBHelper(context);// 初始化数据库
	}

	/**
	 * 插入一个Task
	 */
	public void insert(Task t) {
		SQLiteDatabase db = helper.getWritableDatabase(); // 获取到数据库
		db.execSQL("INSERT INTO Task(qid,page,size,appearTimes,right,rightIndex,totalCount) VALUES(?,?,?,?,?,?,?)",
				new Object[] { t.getQid(), t.getPage(), t.getSize(),
						t.getAppearTimes(),t.getRight(),t.getRightIndex(),t.getTotalCount()});
		db.close();
	}


	/**
	 * 此方法最好是一个地方调用 以便维护方便 就是在 每次答完题的时候 调用即可
	 * 更新Task
	 */
	public void update(Task t) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(
				"UPDATE Task SET qid=?,page=?,size=?,appearTimes=? ,right =?,rightIndex =?",
				new Object[] { t.getQid(), t.getPage(), t.getSize(),
						t.getAppearTimes() ,t.getRight(),t.getRightIndex()});
		db.close();
	}
	public void reset( ) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(
				"UPDATE Task SET qid=?,page=?,size=?,appearTimes=?  ",
				new Object[] { 1, 0,10,2 });
		
		db.execSQL("UPDATE Question SET  appearTimes=2 " );  
		db.close();
	}
	/**
	 *  查找
	 * @return 对应的对象, 如果未找到返回null
	 */
	public Task get() {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"SELECT page,size,appearTimes,rightIndex,qid,totalCount FROM Task limit 1", null);
		Task t = null;
		if (cursor.moveToNext()) {
			Integer page = cursor.getInt(0);
			Integer size = cursor.getInt(1);
			Integer appearTimes = cursor.getInt(2);
			Integer rightIndex = cursor.getInt(3);
			Integer qid = cursor.getInt(4);
			Integer totalCount = cursor.getInt(5);
			 
			t = new Task(page, size, appearTimes,rightIndex,qid,totalCount, find(qid),false);
		}

		if (t == null) {
			cursor= db.rawQuery("SELECT COUNT(*) FROM Question", null);  
			cursor.moveToNext();  
            int count =  cursor.getInt(0);
			t = new Task(1, 10, 2, null,1,count,find(1),false);
			insert(t);
		}
		cursor.close();
		db.close();
		return t;
	}
	   public Question find(Integer id) {  
           SQLiteDatabase db = helper.getReadableDatabase();  
           Cursor cursor = db.rawQuery("SELECT title,answer,appearTimes FROM Question WHERE id=?", new String[] { id.toString() });  
           Question q = null;  
           if (cursor.moveToNext()) {  
               String title = cursor.getString(0);  
               String answer = cursor.getString(1);  
               Integer appearTimes = cursor.getInt(2);  
               q = new Question(id, title, answer, appearTimes);  
           }  
           cursor.close();  
           db.close();  
           return q;  
       }

	public void updatePage(int page) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(
				"UPDATE Task SET  page=? ",
				new Object[] { page});
		
		db.close();
		
	}  
}
